package com.yq.cp.controller.basics;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yq.cp.config.UserThreadLocal;
import com.yq.cp.entity.basics.OrganizationalStructure;
import com.yq.cp.entity.basics.SecureManagement;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.service.basics.OrganizationalStructureService;
import com.yq.cp.service.basics.SecureManagementService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 安全培训管理 前端控制器
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/secureManagement")
public class SecureManagementController {
    @Resource
    SecureManagementService secureManagementService;
    @Resource
    OrganizationalStructureService organizationalStructureService;
    /**
     * 添加编辑
     * @param secureManagement
     * @return
     */
    @PostMapping("add")
    public ApiResult add(@RequestBody SecureManagement secureManagement){
        if (Objects.isNull(secureManagement.getId()))
            secureManagement.setCreateUserId(UserThreadLocal.get().getId());
        secureManagementService.saveOrUpdate(secureManagement);
        return ApiResult.success();
    }

    /**
     * 删除
     * @param secureManagement
     * @return
     */
    @PostMapping("remove")
    public ApiResult remove(@RequestBody SecureManagement secureManagement){
        secureManagementService.removeById(secureManagement);
        return ApiResult.success();
    }

    /**
     * 列表
     * @param vo
     * @return
     */
    @PostMapping("list")
    public ApiResult list(@RequestBody PageVo vo){
        Page<SecureManagement> page = new Page<>(vo.getPage(), vo.getSize());
        QueryWrapper<SecureManagement> wrapper = new QueryWrapper<>();
        if (!Objects.isNull(vo.getLike())&&!"".equals(vo.getLike()))
            wrapper.like("title",vo.getLike());
        wrapper.eq("site_id",vo.getSiteId());
        wrapper.orderByDesc("create_time");
        page = secureManagementService.page(page,wrapper);
        if (!page.getRecords().isEmpty()){
            for (SecureManagement secureManagement : page.getRecords()) {
                OrganizationalStructure byId = organizationalStructureService.getById(secureManagement.getSiteId());
                secureManagement.setSiteName(byId.getName());
            }
        }
        return ApiResult.success(page);
    }
}

